/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.Account;

/**
 *
 * @author Student
 */
public class DaoAccount {

    /**
     *
     * checks if user is valid or not
     *
     * @param name username
     * @param password password
     * @return true if exists, false if not
     * @throws SQLException
     */
    public Account authenticateLogin(String name, String password) throws SQLException {
        Account acct = null;

        Connector connect = new Connector();
        Connection conn = connect.getConn();

//sql statement
        PreparedStatement ps = conn.prepareStatement(" select user_id, username, b.ATYPE as account_type, uname, email from ROOT.ACCOUNT_USER  a "
                + " inner join ACCOUNT_TYPE b "
                + " on a.ACCOUNT_TYPE = b.ACCOUNT_TYPE_ID "
                + " where username=? and password=? ");
        ps.setString(1, name);
        ps.setString(2, password);

        ResultSet rs = ps.executeQuery();
//if was able to get a row
        while (rs.next()) {
            acct = new Account(rs.getInt("user_id"), rs.getString("username"), rs.getString("account_type"), rs.getString("uname"), rs.getString("email"));
        }

        return acct;

    }

    public boolean registerAccount(Account acct, String password) throws SQLException {
        Connector connect = new Connector();
        Connection conn = connect.getConn();

        PreparedStatement ps = conn.prepareStatement("INSERT INTO ROOT.ACCOUNT_USER "
                + "(USERNAME, PASSWORD, ACCOUNT_TYPE, UNAME, EMAIL) "
                + "VALUES (?,?,?,?,?) ");

        ps.setString(1, acct.getUsername());
        ps.setString(2, password);
        ps.setInt(3, 2);
        ps.setString(4, acct.getName());
        ps.setString(5, acct.getEmail());


        return ps.executeUpdate() != 0 ? true : false;


    }
    
   
    
    
    
    
}
